import 'package:dio/dio.dart';
import 'http.dart';
import 'http_exceptions.dart';
import 'http_global.dart';
import 'response_model/response_model.dart';

class HttpUtils {

  // static void init(
  //     {String? baseUrl,
  //     int? connectTimeout,
  //     int? receiveTimeout,
  //     List<Interceptor>? interceptors}) {
  //   Http().init(
  //       baseUrl: baseUrl,
  //       connectTimeout: connectTimeout,
  //       receiveTimeout: receiveTimeout,
  //       interceptors: interceptors);
  // }

  static void setHeaders(Map<String, dynamic> map) {
    Http().setHeaders(map);
  }

  static void cancelRequests({CancelToken? token}) {
    Http().cancelRequests(token: token);
  }

  // static deleteCookieAll() async {
  //   Http().deleteCookieAll();
  // }
  //
  // static deleteCookie(Uri uri) async {
  //   Http().deleteCookie(uri);
  // }

  static Future<ResponseModel<T>> get<T>(
    String path, {
    T Function(Map<String, dynamic>? json)? fromJsonT,
    T Function(List<dynamic>? json)? fromListJsonT,
    Map<String, dynamic>? params,
    Options? options,
    CancelToken? cancelToken,
    bool refresh = false,
    bool noCache = !HttpGlobal.cacheEnable,
    String? cacheKey,
    bool cacheDisk = false,
  }) async {
    try {
      Response response = await Http().get(
        path,
        params: params,
        options: options,
        cancelToken: cancelToken,
        refresh: refresh,
        noCache: noCache,
        cacheKey: cacheKey,
      );
      var json = response.data;
      if (fromJsonT != null) {
        return ResponseModel.fromJson(
          json,
          (json) => fromJsonT(json),
        );
      }
      if (fromListJsonT != null) {
        return ResponseModel.fromListJson(
          json,
          (json) => fromListJsonT(json),
        );
      }
      return ResponseModel.noDataSucceed(json);
    } on DioException catch (e) {
      //error统一处理
      AppException appException = AppException.create(e);
      return ResponseModel.err(appException);
    }
  }

  static Future<dynamic> getNoModel(
    String path, {
    Map<String, dynamic>? params,
    Options? options,
    CancelToken? cancelToken,
    bool refresh = false,
    bool noCache = !HttpGlobal.cacheEnable,
    String? cacheKey,
    bool cacheDisk = false,
  }) async {
    try {
      Response response = await Http().get(
        path,
        params: params,
        options: options,
        cancelToken: cancelToken,
        refresh: refresh,
        noCache: noCache,
        cacheKey: cacheKey,
      );
      return response.data;
    } on DioException catch (e) {
      //error统一处理
      AppException appException = AppException.create(e);
      return ResponseModel.err(appException);
    }
  }

  static Future<ResponseModel<T>> post<T>(
    String path, {
    T Function(Map<String, dynamic>? json)? fromJsonT, //需要解析的对象
    T Function(List<dynamic>? json)? fromListJsonT, //需要解析的List对象
    dynamic data,
    Map<String, dynamic>? params, //参数
    Options? options, //配置参数
    CancelToken? cancelToken, //请求标记
  }) async {
    try {
      Response response = await Http().post(
        path,
        data: data,
        params: params,
        options: options,
        cancelToken: cancelToken,
      );
      var json = response.data;
      //解析{}
      if (fromJsonT != null) {
        return ResponseModel.fromJson(
          json,
          (json) => fromJsonT(json),
        );
      }
      //解析[]
      if (fromListJsonT != null) {
        return ResponseModel.fromListJson(
          json,
          (json) => fromListJsonT(json),
        );
      }
      //不需要解析的
      return ResponseModel.noDataSucceed(json);
    } on DioException catch (e) {
      //error统一处理
      AppException appException = AppException.create(e);
      return ResponseModel.err(appException);
    }
  }

  static Future<ResponseModel<T>> put<T>(
    String path, {
    T Function(Map<String, dynamic>? json)? fromJsonT, //需要解析的对象
    T Function(List<dynamic>? json)? fromLisJsonT, //需要解析的List对象
    dynamic data,
    Map<String, dynamic>? params,
    Options? options,
    CancelToken? cancelToken,
  }) async {
    try {
      Response response = await Http().put(
        path,
        data: data,
        params: params,
        options: options,
        cancelToken: cancelToken,
      );
      //解析{}
      if (fromJsonT != null) {
        return ResponseModel.fromJson(
          response.data,
          (json) => fromJsonT(json),
        );
      }
      //解析[]
      if (fromLisJsonT != null) {
        return ResponseModel.fromListJson(
          response.data,
          (json) => fromLisJsonT(json),
        );
      }
      //不需要解析的
      return ResponseModel.noDataSucceed(response.data);
    } on DioException catch (e) {
      //error统一处理
      AppException appException = AppException.create(e);
      return ResponseModel.err(appException);
    }
  }

  static Future<ResponseModel<T>> patch<T>(
    String path, {
    T Function(Map<String, dynamic>? json)? fromJsonT, //需要解析的对象
    T Function(List<dynamic>? json)? fromLisJsonT, //需要解析的List对象
    dynamic data,
    Map<String, dynamic>? params,
    Options? options,
    CancelToken? cancelToken,
  }) async {
    try {
      Response response = await Http().patch(
        path,
        data: data,
        params: params,
        options: options,
        cancelToken: cancelToken,
      );
      //解析{}
      if (fromJsonT != null) {
        return ResponseModel.fromJson(
          response.data,
          (json) => fromJsonT(json),
        );
      }
      //解析[]
      if (fromLisJsonT != null) {
        return ResponseModel.fromListJson(
          response.data,
          (json) => fromLisJsonT(json),
        );
      }
      //不需要解析的
      return ResponseModel.noDataSucceed(response.data);
    } on DioException catch (e) {
      //error统一处理
      AppException appException = AppException.create(e);
      return ResponseModel.err(appException);
    }
  }

  static Future<ResponseModel<T>> delete<T>(
    String path, {
    T Function(Map<String, dynamic>? json)? fromJsonT, //需要解析的对象
    T Function(List<dynamic>? json)? fromLisJsonT, //需要解析的List对象
    dynamic data,
    Map<String, dynamic>? params,
    Options? options,
    CancelToken? cancelToken,
  }) async {
    try {
      Response response = await Http().delete(
        path,
        data: data,
        params: params,
        options: options,
        cancelToken: cancelToken,
      );
      //解析{}
      if (fromJsonT != null) {
        return ResponseModel.fromJson(
          response.data,
          (json) => fromJsonT(json),
        );
      }
      //解析[]
      if (fromLisJsonT != null) {
        return ResponseModel.fromListJson(
          response.data,
          (json) => fromLisJsonT(json),
        );
      }
      //不需要解析的
      return ResponseModel.noDataSucceed(response.data);
    } on DioException catch (e) {
      //error统一处理
      AppException appException = AppException.create(e);
      return ResponseModel.err(appException);
    }
  }

  static Future<ResponseModel<T>> postForm<T>(
    String path, {
    T Function(Map<String, dynamic>? json)? fromJsonT, //需要解析的对象
    T Function(List<dynamic>? json)? fromLisJsonT, //需要解析的List对象
    required Map<String, dynamic> params,
    Options? options,
    CancelToken? cancelToken,
  }) async {
    try {
      Response response = await Http().postForm(
        path,
        params: params,
        options: options,
        cancelToken: cancelToken,
      );
      //解析{}
      if (fromJsonT != null) {
        return ResponseModel.fromJson(
          response.data,
          (json) => fromJsonT(json),
        );
      }
      //解析[]
      if (fromLisJsonT != null) {
        return ResponseModel.fromListJson(
          response.data,
          (json) => fromLisJsonT(json),
        );
      }
      //不需要解析的
      return ResponseModel.noDataSucceed(response.data);
    } on DioException catch (e) {
      //error统一处理
      AppException appException = AppException.create(e);
      return ResponseModel.err(appException);
    }
  }

  static Future upload<T>(
    String path, {
    Options? options,
    dynamic data,
    Map<String, dynamic>? params,
    CancelToken? cancelToken,
    Function(int count, int total)? onSendProgress,
  }) async {
    try {
      return await Http().upload(
        path,
        data: data,
        params: params,
        options: options,
        cancelToken: cancelToken,
        onSendProgress: onSendProgress,
      );
    } on DioException catch (e) {
      //error统一处理
      AppException appException = AppException.create(e);
      return ResponseModel.err(appException);
    }
  }

  static Future download<T>(
    String urlPath,
    dynamic savePath, {
    Options? options,
    dynamic data,
    Map<String, dynamic>? params,
    CancelToken? cancelToken,
    Function(int count, int total)? onReceiveProgress,
  }) async {
    try {
      return await Http().download(
        urlPath,
        savePath,
        data: data,
        params: params,
        options: options,
        cancelToken: cancelToken,
        onReceiveProgress: onReceiveProgress,
      );
    } on DioException catch (e) {
      //error统一处理
      //return ResponseModel.err(AppException.create(e));
      return null;
    }
  }

  static Future gerRequest(
    String url, {
    dynamic data,
    CancelToken? cancelToken,
    Map<String, dynamic>? params,
  }) async {
    try {
      return await Http().getRequest(
        url,
        data: data,
        params: params,
        cancelToken: cancelToken,
      );
    } on DioException catch (e) {
      //error统一处理
      //return ResponseModel.err(AppException.create(e));
      return null;
    }
  }

  static Future postRequest(
    String url,
    String method, {
    dynamic data,
    CancelToken? cancelToken,
    Map<String, dynamic>? params,
  }) async {
    try {
      return await Http().postRequest(
        url,
        method,
        data: data,
        params: params,
        cancelToken: cancelToken,
      );
    } on DioException catch (e) {
      //error统一处理
      //return ResponseModel.err(AppException.create(e));
      return null;
    }
  }
}
